package com.way.cloud.service.impl;

import com.way.cloud.domain.Customer;
import com.way.cloud.repository.CustomerRepository;
import com.way.cloud.service.CustomerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerRepository repository;

    @Override
    public Flux<Customer> findAllCustomers() {
        return repository.findAll();
    }

    @Override
    public Mono<Customer> findCustomerById(Integer id) {
        return repository.findById(id);
    }

    @Override
    public Flux<Customer> findCustomerByLastName(String lastName) {
        return repository.findByLastName(lastName);
    }

    @Override
    public Mono<Customer> saveCustomer(Customer customer) {
        return repository.save(customer);
    }

    @Override
    public Mono<Void> updateCustomer(Integer id, Customer customer) {
        return repository.findById(id)
                .flatMap(existing -> {
                    existing.setFirstName(customer.getFirstName());
                    existing.setLastName(customer.getLastName());
                    return repository.save(existing);
                })
                .then();
    }

    @Override
    public Mono<Void> deleteCustomerById(Integer id) {
        return repository.deleteById(id);
    }

}
